package com.kou.controller;/**
 * @author SteinY
 * @create 2022-03-24 21:32
 * @description:
 */

import com.kou.pojo.Address;
import com.kou.pojo.User;
import com.kou.service.AddressService;
import com.kou.service.impl.AddressServiceImpl;
import com.kou.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * projectName:myshop2
 * author:SteinY
 * time:2022/3/24 21:32
 * description:
 */
@WebServlet("/address")
public class AddressServlet extends BaseServlet {
    AddressService addressService = new AddressServiceImpl();
    public String show(HttpServletRequest request, HttpServletResponse response){
        String uid = request.getParameter("uid");
        List<Address> addresses = addressService.findAll(uid);
        request.setAttribute("list",addresses);
        return Constants.FORWARD + "self_info.jsp";
    }

    public String add(HttpServletRequest request, HttpServletResponse response){
        String uid = request.getParameter("uid");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        addressService.add(uid,name,phone,detail);

        return Constants.FORWARD + "address?method=show&uid="+uid;
    }

    public String delete(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("aid");
        addressService.delete(id);
        User user =(User) request.getSession().getAttribute("loginUser");
        int uid=user.getId();
        return Constants.FORWARD + "address?method=show&uid="+uid;
    }

    public String update(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        String state = request.getParameter("state");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        addressService.update(id,state,name,phone,detail);
        User user =(User) request.getSession().getAttribute("loginUser");
        int uid=user.getId();
        return Constants.FORWARD + "address?method=show&uid="+uid;
    }
    public String setDefault(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("aid");
        addressService.setDefault(id);
        User user =(User) request.getSession().getAttribute("loginUser");
        int uid=user.getId();
        return Constants.FORWARD + "address?method=show&uid="+uid;
    }
}